package com.zwh.controller;


import com.zwh.entity.Patient;
import com.zwh.entity.User;
import com.zwh.service.UserService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping()
public class UserController extends HttpServlet {

//    //登陆前首页跳转
//    @GetMapping("homef")
//    public String homef() {
//        return "homef";
//    }
//    //登陆后首页跳转
//    @GetMapping("homeb")
//    public String homeb() {
//        return "homeb";
//    }
    //MvcConfig里写有

    //科室导航界面跳转
    @GetMapping("departments")
    public String departments(){
        return "departments";
    }

    //医生团队界面跳转
    @GetMapping("doctors")
    public String doctors(){
        return "doctors";
    }
    //预约挂号界面跳转
    @GetMapping("appointment")
    public String appointment(){
        return  "appointment";
    }
    //关于我们界面跳转
    @GetMapping("about")
    public  String about(){
        return "about";
    }


    @Autowired
    private UserService userService;

    //用户登录
    @RequestMapping("login")//添加注解使方法可以处理HTTP请求,这里我把value省略了
    public void login(String name, String password, HttpSession session, HttpServletResponse response) throws IOException {
        try {
            //调用业务层进行登录
            User user = userService.login(name, password);
            //在seesion中保存用户信息 名字 密码
            session.setAttribute("user",user);
            //设置session过期时间(40分钟）
            session.setMaxInactiveInterval(40*60);
            //重定向到受保护界面
            response.sendRedirect("homeb");

        } catch (Exception e) {
            e.printStackTrace();
            //验证失败就返回登录界面
            response.sendRedirect("homef");
        }

    }


    //用户登录
    @RequestMapping( "regist")//value = "login" 表示这个方法处理 /login 路径的请求
    public String regist(User user) {
        try {
            // 注册用户
            userService.regist(user);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return "redirect:/homef";
        }
        return "redirect:/homeb";
    }

    //用户的表单提交
    @RequestMapping("patientkeep")
    public String patientkeep(Patient patient){
        userService.patientkeep(patient);
        return "redirect:/information";
    }

    //在预约单中展示病人信息
    @RequestMapping("/lists")
    public String lists(Model model) {
        List<Patient> patientList = userService.patientlists();
        model.addAttribute("patientList", patientList);
        return "information";
    }

    //退出登录
    @RequestMapping("/logout")
    public void logout(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //获取session
        HttpSession session = request.getSession(false);
        if (session != null){
            session.invalidate();
        }
        response.sendRedirect("homef");
    }

}
